package com.jz.community.moduleshoppingguide.nearshop.ui.adapter;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jz.community.basecomm.glideConfig.GlideApp;
import com.jz.community.basecomm.utils.RxDataTool;
import com.jz.community.basecomm.utils.RxTimeTool;
import com.jz.community.basecomm.utils.SHelper;
import com.jz.community.moduleshoppingguide.R;
import com.jz.community.moduleshoppingguide.nearshop.bean.ShopDownStairsBean;
import com.jz.community.moduleshoppingguide.nearshop.ui.activity.NearShopGoodsActivity;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Ani_H on 2018/3/6.
 */

public class NearShopAdapter extends BaseQuickAdapter<ShopDownStairsBean.EmbeddedBeanX.ContentBean, BaseViewHolder> {

    public NearShopAdapter(int layoutResId, @Nullable List<ShopDownStairsBean.EmbeddedBeanX.ContentBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, ShopDownStairsBean.EmbeddedBeanX.ContentBean entity) {
        //店铺头像
        CircleImageView avatar = helper.getView(R.id.near_shop_main_avatar);
        //休息图片
        CircleImageView avatar_rest = helper.getView(R.id.near_shop_main_rest);
        int status = 1;
        if (!TextUtils.isEmpty(entity.getBusinessStartHours()) && !TextUtils.isEmpty(entity.getBusinessEndHours())) {
            try {
                if (RxTimeTool.compare(entity.getBusinessStartHours())) {
                    status = 0;
                } else if (RxTimeTool.compare(entity.getBusinessEndHours())) {
                    status = 1;
                } else {
                    status = 0;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //0 休息 1 营业
        if (status == 1) {
            //营业
            avatar_rest.setVisibility(View.GONE);
            GlideApp.with(mContext)
                    .load(entity.getLogo())
                    .placeholder(R.mipmap.near_shop_avatar_default)
                    .error(R.mipmap.near_shop_avatar_default)
                    .into(avatar);
        } else {
            //休息
            avatar_rest.setVisibility(View.VISIBLE);
            GlideApp.with(mContext)
                    .load(entity.getLogo())
                    .placeholder(R.mipmap.near_shop_avatar_default)
                    .error(R.mipmap.near_shop_avatar_default)
                    .into(avatar);
        }
        helper.setText(R.id.near_shop_main_name, entity.getName());

        helper.setText(R.id.near_shop_main_num, "在售商品" + entity.getCount() + "件");

//        MaterialRatingBar ratingBar = helper.getView(R.id.near_shop_main_rating);
//        float rating = Float.parseFloat(entity.getShop_score());
//        //评分
//        ratingBar.setRating(rating);
        //距离
        helper.setText(R.id.near_shop_main_distance, "距" + RxDataTool.format2Decimals1(entity.getDistance() + "") + "m");
        //商品列表
        RecyclerView goodsRv = helper.getView(R.id.near_shop_main_rv);
        RelativeLayout shop_item_top = helper.getView(R.id.rl_near_shop_item_top);
        LinearLayout near_shop_main_rv_parent = helper.getView(R.id.near_shop_main_rv_parent);
        if (null != entity.get_embedded().getGoodsInfoViewList() && entity.get_embedded().getGoodsInfoViewList().size() > 0) {
            near_shop_main_rv_parent.setVisibility(View.VISIBLE);
            SHelper.vis(shop_item_top);
            LinearLayoutManager layoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);
            goodsRv.setHasFixedSize(true);
            goodsRv.setFocusable(false);
            goodsRv.setLayoutManager(layoutManager);
            final List<ShopDownStairsBean.EmbeddedBeanX.ContentBean.EmbeddedBean.GoodsInfoViewListBean> goodsList = entity.get_embedded().getGoodsInfoViewList();
            final NearShopGoodsAdapter adapter_goods = new NearShopGoodsAdapter(R.layout.module_shoppingguide_item_near_shop_goods, goodsList, entity);
            goodsRv.setAdapter(adapter_goods);
        } else {
            near_shop_main_rv_parent.setVisibility(View.GONE);
            SHelper.gone(shop_item_top);
        }
        //第一个Item没有上边距
        LinearLayout main_parent = helper.getView(R.id.near_shop_main_parent);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        int mg_top = SHelper.dp2px(mContext, 10);

        if (helper.getAdapterPosition() == 0) {
            lp.setMargins(0, 0, 0, 0);
            main_parent.setLayoutParams(lp);
        } else {
            lp.setMargins(0, mg_top, 0, 0);
            main_parent.setLayoutParams(lp);
        }
        shop_item_top.setOnClickListener(v -> {
            Intent intent = new Intent(mContext, NearShopGoodsActivity.class);
            intent.putExtra("shopId", entity.getId() + "");
            intent.putExtra("gid", "");
            mContext.startActivity(intent);
        });
    }


}
